package com.example.springbootexam.web;

import com.example.springbootexam.bean.Result;
import com.example.springbootexam.bean.User;
import com.example.springbootexam.bean.UserInfo;
import com.example.springbootexam.biz.BizExeption;
import com.example.springbootexam.biz.UserBiz;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.Errors;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpSession;
import javax.validation.Valid;
import java.io.File;
import java.io.IOException;

/**
 * Company 源辰信息
 *
 * @author navy
 * @date 2024/8/12
 * Email haijunzhou@hnit.edu.cn
 */
@RestController
public class UserDo {

    @Autowired
    UserBiz userBiz;
    @RequestMapping("reg")
    public Result reg(@Valid @RequestBody UserInfo user, Errors errors){
        // 自定义判断
        if (user.getRepwd()==null) {
            errors.rejectValue("repwd", "is_empty", "确认密码不能为空!");
        }
        if (user.getRepwd().equals(user.getPwd()) == false) {
            errors.rejectValue("repwd", "not_equals", "确认密码与密码不一致!");
        }
        if (errors.hasErrors()) {
            return new Result(0, "注册信息错误!", errors.getAllErrors());
        }
        try {
            userBiz.reg(user);
            return new Result(1,"注册成功!",null);
        } catch (BizExeption e) {
            String field = e.getData().toString();
            errors.rejectValue(field,"??", e.getMessage());
            return new Result(0, "注册信息错误!", errors.getAllErrors());
        }
    }

    @PostMapping("upload")
    public Result upload(@RequestParam("image") MultipartFile file) throws IOException {
        System.out.println("字段名 file.getName() = " + file.getName());
        System.out.println("文件名 file.getOriginalFilename() = " + file.getOriginalFilename());
        System.out.println("file.getSize() = " + file.getSize());
        File fileObj = new File("E:\\QQ\\" + file.getOriginalFilename());
        file.transferTo(fileObj);
        //
        String webPath = "head/" + file.getOriginalFilename();
        return new Result(1, "文件上传成功!", webPath);
    }

    @RequestMapping("signIn")
    public Result login(@Valid @RequestBody User user, Errors errors,
                        HttpSession session){
        if (errors.hasFieldErrors("name") || errors.hasFieldErrors("pwd")) {
            return new Result(0,"字段验证错误!",errors.getAllErrors() );
        }
        try {
            User dbUser = userBiz.login(user);
            session.setAttribute("myinfo", dbUser);
            dbUser.setPwd(null);
            String phone = dbUser.getPhone().substring(0,3)+ "****" +
                           dbUser.getPhone().substring(8);
            dbUser.setPhone(phone);
        } catch (BizExeption e) {
            errors.rejectValue(e.getData().toString(),
                    "name.error", e.getMessage());
            return new Result(0,"字段验证错误!",errors.getAllErrors());
        }
        Object requestURI = session.getAttribute("requestURI");
        // 清除该地址
        session.removeAttribute("requestURI");
        return new Result(1,"登录成功", requestURI);
    }

    @RequestMapping("signOut")
    public ModelAndView logout(ModelAndView mav, HttpSession session){
        session.invalidate();
        mav.setViewName("redirect:/");
        return mav;
    }

    @RequestMapping("myinfo")
    public Result myinfo( @SessionAttribute(required = false) User myinfo){
        if(myinfo == null){
            return new Result(0,"未登录",null);
        } else {
            return new Result(1,"已登录",myinfo);
        }
    }

    @RequestMapping("updatePwd")
    public Result updatePwd(@Valid @RequestBody UserInfo user, Errors errors){
        if (errors.hasFieldErrors("name") || errors.hasFieldErrors("pwd") || errors.hasFieldErrors("repwd")) {
            return new Result(0,"字段验证错误!",errors.getAllErrors());
        }
        if (user.getRepwd().equals(user.getPwd()) == false) {
            errors.rejectValue("repwd", "not_equals", "两次密码不一致!");
            return new Result(0,"字段验证错误!",errors.getAllErrors());
        }

        try {
            userBiz.updatePwd(user);
            return new Result(1,"密码修改成功!",null);
        } catch (BizExeption e) {
            errors.rejectValue(e.getData().toString(),
                    "pwd.error", e.getMessage());
            return new Result(0,"修改密码失败!",errors.getAllErrors());
        }
    }


}

